﻿namespace DimensionsHelper.LanguageService.Syntax;

/// <summary>
///     赋值表达式
/// </summary>
public class AssignmentExpression : BinaryExpression
{
    private SyntaxToken? _setKeywordToken;

    internal AssignmentExpression(Expression leftOperand, SyntaxToken oper, SyntaxNode? parent) :
        base(SyntaxKinds.AssignmentExpression, leftOperand, oper, parent)
    {
    }


    /// <summary>
    ///     Set 关键字
    /// </summary>
    public SyntaxToken? SetKeywordToken => _setKeywordToken;


    public AssignmentExpression Set(SyntaxToken setKeywordToken)
    {
        SetChildNullableField(ref _setKeywordToken, setKeywordToken);
        return this;
    }


    public override void AcceptVisitor<T>(SyntaxNodeVisitor<T> visitor)
    {
        Accept(visitor, SetKeywordToken);
        base.AcceptVisitor(visitor);
    }
}


public static partial class NodeFactory
{
    public static AssignmentExpression CreateAssignmentExpression(SyntaxNode? parent, SyntaxToken oper,
        Expression left, Expression? right = null, SyntaxToken? setKeyword = null)
    {
        AssignmentExpression expr = new(left, oper, parent);

        if (right != null)
        {
            expr.Update(right);
        }

        if (setKeyword != null)
        {
            expr.Set(setKeyword);
        }

        return expr;
    }
}